package com.work.shart.web.controller.goods;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.work.shart.common.mybatis.service.impl.SuperServiceImpl;
import com.work.shart.common.result.ResultVo;

import com.work.shart.web.entity.goods.GoodsCommentEntity;


import com.work.shart.web.service.goods.IGoodsCommentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.work.shart.common.result.Result.success;

@RestController
@RequestMapping("/api/goods/comment")
@Slf4j
@Api(tags = "文章评论")
public class GoodsCommnetController extends SuperServiceImpl<IGoodsCommentService, GoodsCommentEntity> {


    @ApiOperation(value = "获取评论信息")
    @GetMapping("commnetList")
    public ResultVo geta() {
        List<GoodsCommentEntity> result = Lists.newArrayList();
        Map<Long, GoodsCommentEntity> map = Maps.newHashMap();
        List<GoodsCommentEntity> list = getBaseService().list();
        list.forEach(goodsCommentEntity -> {
            if (goodsCommentEntity.getParentId() == null) {
                result.add(goodsCommentEntity);
            }
            map.put(goodsCommentEntity.getId(), goodsCommentEntity);
        });
        list.forEach(goodsCommentEntity -> {
            if (goodsCommentEntity.getParentId() != null) {
                GoodsCommentEntity comment = map.get(goodsCommentEntity.getParentId());
                if (comment.getChild() == null) {
                    comment.setChild(new ArrayList<>());
                }
                comment.getChild().add(goodsCommentEntity);
            }
        });

        return success(result);
    }

    @ApiOperation(("发布评论"))
    @PostMapping("comment")
    public ResultVo comment(@Validated @RequestBody GoodsCommentEntity goodsCommentEntity) {
        getBaseService().save(goodsCommentEntity);
        return success();
    }


}
